package com.chenfan.process.controller;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chenfan.ccp.common.result.R;
import com.chenfan.process.biz.ApprovalInternalBiz;
import com.chenfan.process.dto.ApprovalMapDTO;
import com.chenfan.process.dto.ApprovalOperationDTO;
import com.chenfan.process.dto.CheckPermissionDTO;
import com.chenfan.process.dto.StartApprovalDTO;
import com.chenfan.process.vo.ApprovalNodeVO;
import com.chenfan.process.vo.ApprovalOperationVO;
import com.chenfan.process.vo.StartApprovalVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author liuming
 * @date 2021/10/11
 */
@Api(tags = "审批 - 服务内容之间用")
@RestController
@RequestMapping("/approval/api")
public class ApprovalInternalController {

    @Autowired
    private ApprovalInternalBiz approvalInternalBiz;

    @ApiOperation("审批状态变更")
    @PostMapping("/approval-change")
    public R<List<ApprovalOperationVO>> approvalChange(@Valid @RequestBody List<ApprovalOperationDTO> approvalList)
        throws ExecutionException, InterruptedException {
        return R.ok(approvalInternalBiz.batchApproval(approvalList));
    }

    @ApiOperation("批量发起审批流程")
    @PostMapping("/approvalTemplate/{approvalTemplateCode}")
    public R<List<StartApprovalVO>> startBatchApproval(
        @RequestHeader(value = "projectName", required = false) String projectName,
        @PathVariable String approvalTemplateCode, @RequestBody List<StartApprovalDTO> approvalList)
        throws ExecutionException, InterruptedException {
        return R.ok(approvalInternalBiz.startBatchApproval(projectName, approvalTemplateCode, approvalList));
    }

    @ApiOperation("批量查询当前审批节点")
    @PostMapping("/node/batch")
    public R<List<ApprovalNodeVO>> listApprovalNode(@RequestBody ApprovalMapDTO approval) {
        return R.ok(approvalInternalBiz.listCurrentApprovalNode(approval.getApprovalIdList()));
    }

    @ApiOperation("单个查询当前审批节点")
    @GetMapping("/{approvalId}/node")
    public R<ApprovalNodeVO> getApprovalNode(@PathVariable Long approvalId) {
        List<ApprovalNodeVO> approvalNodeVOS = approvalInternalBiz.listCurrentApprovalNode(Arrays.asList(approvalId));
        return R.ok(approvalNodeVOS.get(0));
    }

    @ApiOperation("检查用户是否有审批权限")
    @PostMapping("/check-permission")
    public R<Boolean> checkPermission(@Valid @RequestBody CheckPermissionDTO checkPermission) {
        return R.ok(approvalInternalBiz.checkPermission(checkPermission));
    }
}
